package Service;

import Entity.Workorder;
import Mapper.WorkorderMapper;
import Service.WorkorderService;
import java.time.LocalDateTime;
import java.util.List;

public class WorkorderServiceImpl implements WorkorderService {

    private WorkorderMapper workorderMapper;

    public void setWorkorderMapper(WorkorderMapper workorderMapper) {
        this.workorderMapper = workorderMapper;
    }

    @Override
    public boolean save(Workorder workorder) {
        // 基本校验
        if (workorder.getNo() == null) {
            throw new IllegalArgumentException("工单编号不能为空");
        }
        if (workorder.getCostomer_id() == null) {
            throw new IllegalArgumentException("必须指定客户ID");
        }
        if (workorder.getCreator_id() == null) {
            throw new IllegalArgumentException("必须指定创建人ID");
        }

        // 设置默认值
        if (workorder.getStatus() == null) {
            workorder.setStatus(0); // 0表示新建状态
        }
        if (workorder.getCreate_time() == null) {
            workorder.setCreate_time(LocalDateTime.now());
        }

        return workorderMapper.insert(workorder) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return workorderMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(Workorder workorder) {
        if (workorder.getId() == null) {
            throw new IllegalArgumentException("更新工单必须指定ID");
        }
        return workorderMapper.update(workorder) > 0;
    }

    @Override
    public Workorder getById(Integer id) {
        return workorderMapper.selectById(id);
    }

    @Override
    public List<Workorder> getAll() {
        return workorderMapper.selectAll();
    }

    @Override
    public List<Workorder> getByCustomerId(Integer customerId) {
        return workorderMapper.selectByCustomerId(customerId);
    }

    @Override
    public List<Workorder> getByBusinessId(Integer businessId) {
        return workorderMapper.selectByBusinessId(businessId);
    }

    @Override
    public List<Workorder> getByCreatorId(Integer creatorId) {
        return workorderMapper.selectByCreatorId(creatorId);
    }

    @Override
    public List<Workorder> getByHandlerId(Integer handlerId) {
        return workorderMapper.selectByHandlerId(handlerId);
    }

    @Override
    public List<Workorder> getByStatus(Integer status) {
        return workorderMapper.selectByStatus(status);
    }

    @Override
    public List<Workorder> getRecentOrders(Integer days) {
        return workorderMapper.selectRecentOrders(days);
    }

    @Override
    public boolean assignHandler(Integer workorderId, Integer handlerId) {
        Workorder workorder = workorderMapper.selectById(workorderId);
        if (workorder == null) {
            throw new IllegalArgumentException("工单不存在");
        }
        workorder.setHandler_id(handlerId);
        return workorderMapper.update(workorder) > 0;
    }

    @Override
    public boolean changeStatus(Integer workorderId, Integer newStatus) {
        Workorder workorder = workorderMapper.selectById(workorderId);
        if (workorder == null) {
            throw new IllegalArgumentException("工单不存在");
        }
        workorder.setStatus(newStatus);
        return workorderMapper.update(workorder) > 0;
    }
}